<?php

/**
 *
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_ACL_Property_SupportedPrivilegeSet extends Sabre_DAV_Property 
{

    /**
     * Serializes the property in a DOMDocument 
     * 
     * @param Sabre_DAV_Server $server 
     * @param DOMElement $node 
     * @return void
     */
	private $prefix;
	private $privileges; 
	
	public function __construct($privileges)
	{
		$this->privileges = $privileges;
	}
	
	protected function appendPrivilege($appendTo, $privname, $abstract, $description, $container)
	{
    $doc = $appendTo->ownerDocument;
    $sp = $doc->createElement("$this->prefix:supported-privilege");
    $priv = $doc->createElement("$this->prefix:privilege");
    $privelement = $doc->createElement("$this->prefix:$privname");
    if ($abstract)
    {
    	$absElement = $doc->createElement("$this->prefix:abstract");
    	$sp->appendChild($absElement);
    }
    $desc = $doc->createElement("$this->prefix:description");
    $desc->nodeValue = $description;
    $priv->appendChild($privelement);
    $sp->appendChild($priv);
    $sp->appendChild($desc);
    if (isset($container))
    {
    	foreach ($container as $privname=>$inner)
    	{
    		$abstract = $inner["abstract"];
        $description = $inner["description"];
        if (isset($inner["container"]))
        {
        	$container = $inner["container"];
        } else {
        	$container = null;
        }
    		$priv = $this->appendPrivilege($sp, $privname, $abstract, $description, $container);
    	}
    }
    $appendTo->appendChild($sp);
    return $sp;	
	}
	
	public function getPrivileges()
	{
		return $this->privileges;
	}
	
	
    /**
     * TODO: we need an unserialize for this class.
     * Serializes this property.
     *
     * all
     * 	read
     * 		read-acl
     * 		read-properties
     * 		read-content
     * 	read-current-user-privilege-set
     * 	write
     * 		write-acl
     * 		write-properties
     * 		write-content
     * 		bind
     * 		unbind
     *	unlock
     * 
     * @param Sabre_DAV_Server $server 
     * @param DOMElement $dom 
     * @return void
     */
    public function serialize(Sabre_DAV_Server $server,DOMElement $spsElement) 
    {
        $this->prefix = $server->xmlNamespaces['DAV:'];

        foreach ($this->privileges as $privName=>$inner)
        {
        	$abstract = $inner["abstract"];
        	$description = $inner["description"];
	        if (isset($inner["container"]))
	        {
	        	$container = $inner["container"];
	        } else {
	        	$container = null;
	        }
        	$priv = $this->appendPrivilege($spsElement, $privName, $abstract, $description, $container);
        }
    }
	
}
